package com.twu.biblioteca;

import java.util.ArrayList;

/**
 * Created by theyes on 15/01/2014.
 */
public class ReturnABookMenu extends ReturnItemMenu {

    public ReturnABookMenu(MockBookDataBase db) {
        super(db);
    }

    public void attemptReturnItem() {

        String bookTitle = getBookTitle();
        String authorName = getBookAuthor();

        validateBookDetailsForReturn(bookTitle, authorName);
    }


    public void validateBookDetailsForReturn(String bookTitle, String authorName) {
        ArrayList<LibraryItem> allBooks = itemDB.getAllItems();

        for(int i =0; i < allBooks.size(); i++)
        {
            Book workingBook = (Book)allBooks.get(i);
            if (bookTitle.equals(workingBook.title) && authorName.equals(workingBook.author)) {
                allBooks.get(i).checkIn();
                System.out.println("Thank you for returning the book.");
                return;
            }
        }

        System.out.println("That is not a valid book to return. ");
    }

    private String getBookAuthor() {
        System.out.println("Please enter the name of the author of the book you wish to return");
        return userInputHandle.readInput();
    }

    private String getBookTitle() {
        System.out.println("Please enter the name of the book you wish to return");
        return userInputHandle.readInput();
    }

}
